package cn.com.zonesion.rfiddemo.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.zonesion.rfiddemo.R;
import cn.com.zonesion.rfiddemo.update.UpdateService;
import cn.com.zonesion.rfiddemo.util.APKVersionCodeUtils;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.relayout)
    RelativeLayout relativeLayout;
    @BindView(R.id.tv_soft_version)
    TextView tvSoftVersion;
    /**
     * 当前版本
     */
    private String verName;
    /**
     * 最新版本
     */
    private String v;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);//将ButterKnife和IcMainActivity绑定
        initView();
    }

    private void initView() {
//        String versionCode = String.valueOf(APKVersionCodeUtils.getVersionCode(this));
        verName = APKVersionCodeUtils.getVerName(this);
        tvSoftVersion.setText("版本"+verName);
        new updateApkTask().execute("");
    }
    @OnClick({R.id.btn_id,R.id.btn_ic,R.id.btn_high_frequency,R.id.btn_microwave,R.id.menu})
    public void onclick(View view){
        switch (view.getId()) {
            case  R.id.btn_id:
                Toast.makeText(MainActivity.this, "ID卡", Toast.LENGTH_SHORT).show();
                Intent intent1 = new Intent();
                intent1.setClass(MainActivity.this,IdMainActivity.class);
                startActivity(intent1);
                break;
            case  R.id.btn_ic:
                Toast.makeText(MainActivity.this, "IC卡", Toast.LENGTH_SHORT).show();
                Intent intent2 = new Intent();
                intent2.setClass(MainActivity.this,ICMainActivity.class);
                startActivity(intent2);
                break;
            case  R.id.btn_high_frequency:
                Toast.makeText(MainActivity.this, "高频900M", Toast.LENGTH_SHORT).show();
                Intent intent3 = new Intent();
                intent3.setClass(MainActivity.this,HighFrequencyMainActivity.class);
                startActivity(intent3);
                break;
            case  R.id.btn_microwave:
                Toast.makeText(MainActivity.this, "微波2,4G", Toast.LENGTH_SHORT).show();
                Intent intent4 = new Intent();
                intent4.setClass(MainActivity.this,MicrowaveMainActivity.class);
                startActivity(intent4);
                break;
            case R.id.menu:
                Toast.makeText(MainActivity.this, "关于", Toast.LENGTH_SHORT).show();
                Intent intent5 = new Intent();
                intent5.setClass(MainActivity.this,AboutMainActivity.class);
                intent5.putExtra("verName",verName);
                intent5.putExtra("v",v);
                startActivity(intent5);
        }
    }
    private class updateApkTask extends AsyncTask<String,String,String>{
        String url_version = "http://demo.zhiyun360.com/appstore/xLab/RFIDDemo/RfidDemo.text";
        String url_apk = "http://demo.zhiyun360.com/appstore/xLab/RFIDDemo/RfidDemo.apk";
        @Override
        protected String doInBackground(String... strings) {
            HttpURLConnection connection = null;
            InputStreamReader in = null;
            URL url;
            try {
                url = new URL(url_version);
                connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(30000);
                connection.setRequestMethod("GET");
                connection.setDoInput(true);
                connection.setRequestProperty("ContentType",
                        "text/xml;charset=utf-8");
                in = new InputStreamReader(connection.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(in);
                StringBuffer strBuffer = new StringBuffer();
                String line = null;
                while ((line = bufferedReader.readLine()) != null){
                    strBuffer.append(line);
                }
                connection.disconnect();

                v = strBuffer.toString();
                return v;
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }

        @Override
        protected void onPostExecute(String result) {
            //作UI线程的修改
            if(result.length() != 0 && !result.equals(verName)) {

                AlertDialog.Builder builder = new Builder(MainActivity.this);
                builder.setMessage("检测到新的版本"+result+"是否更新软件");
                builder.setTitle("软件更新");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(MainActivity.this, UpdateService.class);
                        i.putExtra("url", url_apk);
                        MainActivity.this.startService(i);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        }
    }
}
